package com.liwei.blog.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.liwei.blog.dao.UserDao;
import com.liwei.blog.pojo.User;
import com.liwei.blog.service.UserService;

/**
* @author liwei
* @version 创建时间：2018年12月1日 下午2:44:43
* 类说明
*/
@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;
	
	@Transactional
	@Override
	public User saveUser(User user) {
		return userDao.save(user);
	}

	@Transactional
	@Override
	public void removeUser(Long id) {
		userDao.deleteById(id);
	}

	@Transactional
	@Override
	public void removeUsersInBatch(List<User> users) {
		userDao.deleteInBatch(users);
	}
	
	@Transactional
	@Override
	public User updateUser(User user) {
		return userDao.save(user);
	}

	@Override
	public User getUserById(Long id) {
		return userDao.getOne(id);
	}

	@Override
	public List<User> listUsers() {
		return userDao.findAll();
	}

	@Override
	public Page<User> findByUsernameLike(String name, Pageable pageable) {
		// 模糊查询
		name = "%" + name + "%";
		Page<User> users = userDao.findByUsernameLike(name, pageable);
		return users;
	}

	@Override
	public List<User> listUsersByUsernames(Collection<String> usernames) {
		// TODO Auto-generated method stub
		return userDao.findByUsernameIn(usernames);
	}

	@Override
	public User getUserByUsername(String username) {
		// TODO Auto-generated method stub
		return userDao.findByUsername(username);
	}

}
